﻿using System;
using System.Windows.Forms;
using SimpleCalculator;
using System.Text.RegularExpressions;

namespace Simple_Calculator_UI
{
    public partial class CalculatorUI : Form
    {
        private char _operation = '+';
        private string[] _expression;
        private readonly char[] _separators = { '-', '+', '/', '*' };
        private Calc _calculator = new Calc();

        public CalculatorUI()
        {
            InitializeComponent();
        }

        private void Digit1Click(object sender, EventArgs e)
        {
            input.Text += @"1";
        }

        private void Digit2Click(object sender, EventArgs e)
        {
            input.Text += @"2";
        }

        private void Digit3Click(object sender, EventArgs e)
        {
            input.Text += @"3";
        }

        private void Digit4Click(object sender, EventArgs e)
        {
            input.Text += @"4";
        }

        private void Digit5Click(object sender, EventArgs e)
        {
            input.Text += @"5";
        }

        private void Digit6Click(object sender, EventArgs e)
        {
            input.Text += @"6";
        }

        private void Digit7Click(object sender, EventArgs e)
        {
            input.Text += @"7";
        }

        private void Digit8Click(object sender, EventArgs e)
        {
            input.Text += @"8";
        }

        private void Digit9Click(object sender, EventArgs e)
        {
            input.Text += @"9";
        }

        private void Digit0Click(object sender, EventArgs e)
        {
            input.Text += @"0";
        }

        private void ActionMulClick(object sender, EventArgs e)
        {
            input.Text += @"*";
            _operation = '*';
        }

        private void ActionDivClick(object sender, EventArgs e)
        {
            input.Text += @"/";
            _operation = '/';
        }

        private void ActionSubsClick(object sender, EventArgs e)
        {
            input.Text += @"-";
            _operation = '-';
        }

        private void ActionAddClick(object sender, EventArgs e)
        {
            input.Text += @"+";
            _operation = '+';
        }

        private void ClearClick(object sender, EventArgs e)
        {
            input.Text = "";
            
        }

        private void EvaluateClick(object sender, EventArgs e)
        {
            if (!(Regex.Match(input.Text, "[-]?[0-9]+[-+*/][-]?[0-9]+")).Success)
            {
                history.Text = input.Text + @" = " + @"Error";
            }
            else 
            {
                _expression = input.Text.Split(_separators, 3);
                var notFound = true;
                foreach (var item in input.Text.ToCharArray())
	            {
		            switch (item)
	                {
                        case '+': 
                            {
                                _operation = '+';
                                notFound = false;
                                break;
                            }
                        case '-':
                            {
                                _operation = '-';
                                notFound = false;
                                break;
                            }
                        case '*':
                            {
                                _operation = '*';
                                notFound = false;
                                break;
                            }
                        case '/':
                            {
                                _operation = '/';
                                notFound = false;
                                break;
                            }
		                default: break;
	                }
                    if (!notFound) break;
	            }
                try
                {
                    _calculator.ProcessCalculations(_operation, Int32.Parse(_expression[0]), Int32.Parse(_expression[1]));
                    history.Text = _expression[0] + _operation + _expression[1] + @" = " + _calculator.Result;
                }
                catch (Exception exc)
                {
                    history.Text = exc.Message;
                }                
            }
            input.Text = "";
        }

        private void MenuExit(object sender, EventArgs e)
        {
            _calculator = null;
            _expression = null;
            Application.Exit();
        }

        private void OnKeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case '\r':
                    {
                        evaluate.PerformClick();
                        break;
                    }
                case ((char)27):
                    {
                        clear.PerformClick();
                        break;
                    }
                default:
                    break;
            }
        }
    }
}
